package com.hoongs.v1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoongs.v1.entity.Advertise;
import com.hoongs.v1.mapper.AdvertiseMapper;
import com.hoongs.v1.service.AdvertiseService;
import com.hoongs.v1.utils.PResult;
import com.hoongs.v1.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;

@Service
public class AdvertiseServiceImpl implements AdvertiseService {

    public static final String NOT_FOUND_ARTICLE = "没有找到该广告信息！";

    @Resource
    private AdvertiseMapper advertiseMapper;

    @Override
    public Result add(Advertise advertise) {
        if (advertiseMapper.insert(advertise) == 1) {
            return Result.opSuccess();
        } else {
            return Result.opError();
        }
    }

    @Override
    public Result remove(String id) {
        if (!findArticleById(id)) return Result.opError(NOT_FOUND_ARTICLE);
        if (advertiseMapper.deleteById(id) == 1) {
            return Result.opSuccess();
        } else {
            return Result.opError();
        }
    }

    @Override
    public Result update(Advertise advertise) {
        if (!findArticleById(advertise.getId())) return Result.opError(NOT_FOUND_ARTICLE);
        if (advertiseMapper.updateById(advertise) == 1) {
            return Result.opSuccess();
        } else {
            return Result.opError();
        }
    }

    @Override
    public Result selectById(String id) {
        Advertise advertise = advertiseMapper.selectById(id);
        if (advertise == null) return Result.error(NOT_FOUND_ARTICLE);
        return Result.success(advertise);
    }

    @Override
    public Result selectList(String name, String status) {
        QueryWrapper<Advertise> wrapper = new QueryWrapper<Advertise>();
        if (StringUtils.isNotBlank(name)) {
            wrapper.like("name", name);
        }
        if (status != null) {
            wrapper.eq("status", status);
        }
        wrapper.orderBy(true, true, "sortcode");
        Collection<Advertise> list = advertiseMapper.selectList(wrapper);
        return Result.success(list);
    }


    @Override
    public Result selectPageList(Integer page, Integer size, String name, String status) {
        if (!PResult.checkPageQuery(page, size)) return Result.error("分页参数错误");

        Page<Advertise> daoPage = new Page<>(page, size);
        QueryWrapper<Advertise> wrapper = new QueryWrapper<Advertise>();
        if (StringUtils.isNotBlank(name)) {
            wrapper.like("name", name);
        }
        if (status != null) {
            wrapper.eq("status", status);
        }
        IPage<Advertise> advertiseDaoIPage = advertiseMapper.selectPage(daoPage, wrapper);
        PResult rs = new PResult(advertiseDaoIPage);
        return Result.success(rs);
    }

    /**
     * 根据Id查询广告是否存在
     */
    public Boolean findArticleById(String id) {
        Advertise advertise = advertiseMapper.selectById(id);
        return advertise != null;
    }
}
